package EXam.Question16;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;

public class PersonManager {
    public static void main(String[] args) {
        // 1. 将人员信息按顺序存放至ArrayList集合中
        ArrayList<Person> personList = new ArrayList<>();
        personList.add(new Person("张成", 28, "甘肃"));
        personList.add(new Person("刘安", 37, "宁夏"));
        personList.add(new Person("刘墉", 40, "山东"));
        personList.add(new Person("和珅", 35, "北京"));
        personList.add(new Person("和二", 36, "北京"));
        personList.add(new Person("纪晓岚", 39, "山西"));
        personList.add(new Person("刘安", 37, "宁夏")); // 重复人员
        
        System.out.println("1. 初始人员信息：");
        for (Person person : personList) {
            System.out.println(person);
        }
        
        // 2. 随机输出一个人员信息
        System.out.println("\r2. 随机输出一个人员信息：");
        Random random = new Random();
        int randomIndex = random.nextInt(personList.size());
        System.out.println(personList.get(randomIndex));
        
        // 3. 删除该集合中和姓人员
        System.out.println("\n3. 删除和姓人员前：");
        for (Person person : personList) {
            System.out.println(person);
        }
        
        // 使用迭代器安全删除
        Iterator<Person> iterator = personList.iterator();
        while(iterator.hasNext()) {
            Person person = iterator.next();
            if(person.getName().startsWith("和")) {
                iterator.remove();
            }
        }

        
        System.out.println("删除和姓人员后：");
        for (Person person : personList) {
            System.out.println(person);
        }
        
        // 4. 去掉重复人员信息(采用Set集合)
        System.out.println("\n4. 去除重复人员信息前：");
        for (Person person : personList) {
            System.out.println(person);
        }
        Set<Person> personSet =new HashSet<>(personList);
        personList=new ArrayList<>(personSet);
        
        System.out.println("去除重复人员信息后：");
        for (Person person : personList) {
            System.out.println(person);
        }
        
        // 5. 修改纪晓岚的地址信息为河北
        System.out.println("\n5. 修改纪晓岚地址信息前：");
        for (Person person : personList) {
            if ("纪晓岚".equals(person.getName())) {
                System.out.println(person);
                break;
            }
        }
        System.out.println("修改纪晓岚地址信息后：");
        personSet.stream()
                .filter(person -> person.getName().equals("纪晓岚"))
                .forEach(person -> person.setAddress("河北"));
        personSet.forEach(System.out::println);
/*        for (Person person : personList) {
            if ("纪晓岚".equals(person.getName())) {
                person.setAddress("河北");
                break;
            }
        }*/
/*
        System.out.println("修改纪晓岚地址信息后：");
        for (Person person : personList) {
            if ("纪晓岚".equals(person.getName())) {
                System.out.println(person);
                break;
            }
        }*/
        
        // 6. 打印输出大于35岁的所有人员信息
        System.out.println("\n6. 大于35岁的所有人员信息：");
        for (Person person : personList) {
            if (person.getAge() > 35) {
                System.out.println(person);
            }
        }
    }
}